<?php
/**
 * Created by PhpStorm.
 * User: allen
 * Date: 2019/3/8
 * Time: 12:48
 */

namespace app\index\model;

use app\admin\model\MemberComment;
use think\Db;
use think\Model;

class Article extends Model
{
    protected $autoWriteTimestamp = false;

    /**
     * 获取数据
     * @param array $where
     * @param int $pageSize
     * @param string $order
     * @param string $field
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where=[],$pageSize=10,$order='id desc',$field='*'){
        return $this->field($field)
            ->where($where)
            ->order($order)
            ->paginate($pageSize);
    }

    /**
     * 用户留言
     * @param $aid
     * @param $uid
     * @param $content
     * @param string $out
     * @return bool
     */
    public function comments($aid,$uid,$content,&$out=''){
       $comment = new Comments();
       $data = [
           'mid'=>$uid,
           'aid'=>$aid,
           'content'=>$content,
           'create_time'=>time()
       ];

       $validate = new \app\index\validate\Comments();
       if(!$validate->check($data)){
           $out = $validate->getError();
           return false;
       }
       if(!$comment->insert($data)){
           $out = '留言失败';
           return false;
       }
       return true;
    }

    /**
     * @param $aid
     * @param $uid
     * @param string $out
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function like($aid,$uid,&$out=''){
        $article = $this->get($aid);
        if(!$article){
            $out = '文章不存在';
            return false;
        }
        $log = new LikeLog();
        Db::startTrans();
        if($log->check($aid,$uid,$id)){
            $res1 = $log->where('id','eq',$id)->delete();
            $article->love = $article['love']-1;
            $res2 = $article->save();
            if($res2 && $res1){
                $out = '取消点赞';
                Db::commit();
                return true;
            }else{
                $out = '取消点赞失败';
                Db::rollback();
                return false;
            }
        }else{
            $res1 = $log->insert([
                'mid'=>$uid,
                'aid'=>$aid,
                'create_time'=>time()
            ]);
            $article->love = $article['love']+1;
            $res = $article->save();
            if($res && $res1){
                $out = '点赞成功';
                Db::commit();
                return true;
            }else{
                $out = '点赞失败';
                Db::rollback();
                return false;
            }
        }
    }

    /**
     *
     * @param int $id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_reply($id=0){
        $model = new MemberComment();
        $list = $model->getListByArticleId($id);
        return $list;
    }

    /**
     * 解析内容
     * @param $value
     * @return string
     */
    public function getContentAttr($value){
        return htmlspecialchars_decode($value);
    }

    public function getLocation($id=0){
        $info = $this->find($id);
        if($info){
            if($info['pro'] && $info['city'] && $info['area'] && $info['address']){
                return "{$info['pro']},{$info['city']},{$info['city']},{$info['address']}";
            }elseif($info['pro'] && $info['city'] && $info['area']){
                return "{$info['pro']},{$info['city']},{$info['area']}";
            }elseif($info['pro'] && $info['city']){
                return "{$info['pro']},{$info['city']}";
            }elseif($info['pro']){
                return "{$info['pro']}";
            }else{
                return "中国";
            }
        }else{
            return '';
        }
    }

    public function getProAttr($value){
        if($value){
            $pro = new Provinces();
            $info = $pro->where('provinceid','eq',$value)->find();
            return $info['province'];
        }else{
            return '';
        }
    }

    public function getCityAttr($value){
        if($value){
            $pro = new Cities();
            $info = $pro->where('cityid','eq',$value)->find();
            return $info['city'];
        }else{
            return '';
        }
    }

    public function getAreaAttr($value){
        if($value){
            $pro = new Areas();
            $info = $pro->where('areaid','eq',$value)->find();
            return $info['area'];
        }else{
            return '';
        }
    }

    /**
     * 检测过期
     * @param int $id
     * @return bool
     */
    public function checkFinish($id=0){
        $info = $this->find($id);
        if($info && $info['finish_time']>time()){
            return false;
        }
        return true;
    }
}